home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / gnused.zip / GNUSED / SED.C < prev    next >
C/C++ Source or Header  |  1992-11-06  |  38KB  |  1,636 lines

  1. /*  GNU SED, a batch stream editor.
  2.     Copyright (C) 1989-1991 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* TimF@microsoft.com:    7-Aug-92  Port to Microsoft's Windows NT (tm) */
  20.  
  21. #ifdef    WINDOWSNT
  22. #include    <errno.h>
  23. #include    <windows.h>
  24. #endif
  25.  
  26.  
  27. #ifdef __STDC__
  28. #define VOID void
  29. #else
  30. #define VOID char
  31. #endif
  32.  
  33. #define _GNU_SOURCE
  34. #include <ctype.h>
  35. #ifndef isblank
  36. #define isblank(c) ((c) == ' ' || (c) == '\t')
  37. #endif
  38. #include <stdio.h>
  39. #include <regex.h>
  40. #include <getopt.h>
  41. #if defined(STDC_HEADERS)
  42. #include <stdlib.h>
  43. #endif
  44. #if defined(USG) || defined(STDC_HEADERS)
  45. #include <string.h>
  46. #include <memory.h>
  47. #define bcopy(s, d, n) (memcpy((d), (s), (n)))
  48. #else
  49. #include <strings.h>
  50. VOID *memchr();
  51. #endif
  52.  
  53.  
  54. char *version_string = "GNU sed version 1.08";
  55.  
  56. /* Struct vector is used to describe a chunk of a sed program.  There is one
  57.    vector for the main program, and one for each { } pair. */
  58. struct vector {
  59.     struct sed_cmd *v;
  60.     int v_length;
  61.     int v_allocated;
  62.     struct vector *up_one;
  63.     struct vector *next_one;
  64. };
  65.  
  66.  
  67. /* Goto structure is used to hold both GOTO's and labels.  There are two
  68.    separate lists, one of goto's, called 'jumps', and one of labels, called
  69.    'labels'.
  70.    the V element points to the descriptor for the program-chunk in which the
  71.    goto was encountered.
  72.    the v_index element counts which element of the vector actually IS the
  73.    goto/label.  The first element of the vector is zero.
  74.    the NAME element is the null-terminated name of the label.
  75.    next is the next goto/label in the list. */
  76.  
  77. struct sed_label {
  78.     struct vector *v;
  79.     int v_index;
  80.     char *name;
  81.     struct sed_label *next;
  82. };
  83.  
  84. /* ADDR_TYPE is zero for a null address,
  85.    one if addr_number is valid, or
  86.    two if addr_regex is valid,
  87.    three, if the address is '$'
  88.  
  89.    Other values are undefined.
  90.  */
  91.  
  92. #define ADDR_NULL    0
  93. #define ADDR_NUM    1
  94. #define ADDR_REGEX    2
  95. #define ADDR_LAST    3
  96.     
  97. struct addr {
  98.     int    addr_type;
  99.     struct re_pattern_buffer *addr_regex;
  100.     int    addr_number;
  101. };
  102.  
  103.  
  104. /* Aflags:  If the low order bit is set, a1 has been
  105.    matched; apply this command until a2 matches.
  106.    If the next bit is set, apply this command to all
  107.    lines that DON'T match the address(es).
  108.  */
  109.  
  110. #define A1_MATCHED_BIT    01
  111. #define ADDR_BANG_BIT    02
  112.  
  113.  
  114. struct sed_cmd {
  115.     struct addr a1,a2;
  116.     int aflags;
  117.  
  118.     char cmd;
  119.  
  120.     union {
  121.         /* This structure is used for a, i, and c commands */
  122.         struct {
  123.             char *text;
  124.             int text_len;
  125.         } cmd_txt;
  126.  
  127.         /* This is used for b and t commands */
  128.         struct sed_cmd *label;
  129.  
  130.         /* This for r and w commands */
  131.         FILE *io_file;
  132.  
  133.         /* This for the hairy s command */
  134.         /* For the flags var:
  135.            low order bit means the 'g' option was given,
  136.            next bit means the 'p' option was given,
  137.            and the next bit means a 'w' option was given,
  138.               and wio_file contains the file to write to. */
  139.  
  140. #define S_GLOBAL_BIT    01
  141. #define S_PRINT_BIT    02
  142. #define S_WRITE_BIT    04
  143. #define S_NUM_BIT    010
  144.  
  145.         struct {
  146.             struct re_pattern_buffer *regx;
  147.             char *replacement;
  148.             int replace_length;
  149.             int flags;
  150.             int numb;
  151.             FILE *wio_file;
  152.         } cmd_regex;
  153.  
  154.         /* This for the y command */
  155.         unsigned char *translate;
  156.  
  157.         /* For { and } */
  158.         struct vector *sub;
  159.         struct sed_label *jump;
  160.     } x;
  161. };
  162.  
  163. /* Sed operates a line at a time. */
  164. struct line {
  165.     char *text;        /* Pointer to line allocated by malloc. */
  166.     int length;        /* Length of text. */
  167.     int alloc;        /* Allocated space for text. */
  168. };
  169.  
  170. /* This structure holds information about files opend by the 'r', 'w',
  171.    and 's///w' commands.  In paticular, it holds the FILE pointer to
  172.    use, the file's name, a flag that is non-zero if the file is being
  173.    read instead of written. */
  174.  
  175. #define NUM_FPS    32
  176. struct {
  177.     FILE *phile;
  178.     char *name;
  179.     int readit;
  180. } file_ptrs[NUM_FPS];
  181.  
  182.  
  183. #if defined(__STDC__)
  184. # define P_(s) s
  185. #else
  186. # define P_(s) ()
  187. #endif
  188.  
  189. void panic P_((char *str, ...));
  190. char *__fp_name P_((FILE *fp));
  191. FILE *ck_fopen P_((char *name, char *mode));
  192. void ck_fwrite P_((char *ptr, int size, int nmemb, FILE *stream));
  193. void ck_fclose P_((FILE *stream));
  194. VOID *ck_malloc P_((int size));
  195. VOID *ck_realloc P_((VOID *ptr, int size));
  196. char *ck_strdup P_((char *str));
  197. VOID *init_buffer P_((void));
  198. void flush_buffer P_((VOID *bb));
  199. int size_buffer P_((VOID *b));
  200. void add_buffer P_((VOID *bb, char *p, int n));
  201. void add1_buffer P_((VOID *bb, int ch));
  202. char *get_buffer P_((VOID *bb));
  203.  
  204. void compile_string P_((char *str));
  205. void compile_file P_((char *str));
  206. struct vector *compile_program P_((struct vector *vector));
  207. void bad_prog P_((char *why));
  208. int inchar P_((void));
  209. void savchar P_((int ch));
  210. int compile_address P_((struct addr *addr));
  211. void compile_regex P_((int slash));
  212. struct sed_label *setup_jump P_((struct sed_label *list, struct sed_cmd *cmd, struct vector *vec));
  213. FILE *compile_filename P_((int readit));
  214. void read_file P_((char *name));
  215. void execute_program P_((struct vector *vec));
  216. int match_address P_((struct addr *addr));
  217. int read_pattern_space P_((void));
  218. void append_pattern_space P_((void));
  219. void line_copy P_((struct line *from, struct line *to));
  220. void line_append P_((struct line *from, struct line *to));
  221. void str_append P_((struct line *to, char *string, int length));
  222. void usage P_((void));
  223.  
  224. extern char *myname;
  225.  
  226. /* If set, don't write out the line unless explictly told to */
  227. int no_default_output = 0;
  228.  
  229. /* Current input line # */
  230. int input_line_number = 0;
  231.  
  232. /* Are we on the last input file? */
  233. int last_input_file = 0;
  234.  
  235. /* Have we hit EOF on the last input file?  This is used to decide if we
  236.    have hit the '$' address yet. */
  237. int input_EOF = 0;
  238.  
  239. /* non-zero if a quit command has been executed. */
  240. int quit_cmd = 0;
  241.  
  242. /* Have we done any replacements lately?  This is used by the 't' command. */
  243. int replaced = 0;
  244.  
  245. /* How many '{'s are we executing at the moment */
  246. int program_depth = 0;
  247.  
  248. /* The complete compiled SED program that we are going to run */
  249. struct vector *the_program = 0;
  250.  
  251. /* information about labels and jumps-to-labels.  This is used to do
  252.    the required backpatching after we have compiled all the scripts. */
  253. struct sed_label *jumps = 0;
  254. struct sed_label *labels = 0;
  255.  
  256. /* The 'current' input line. */
  257. struct line line;
  258.  
  259. /* An input line that's been stored by later use by the program */
  260. struct line hold;
  261.  
  262. /* A 'line' to append to the current line when it comes time to write it out */
  263. struct line append;
  264.  
  265.  
  266. /* When we're reading a script command from a string, 'prog_start' and
  267.    'prog_end' point to the beginning and end of the string.  This
  268.    would allow us to compile script strings that contain nulls, except
  269.    that script strings are only read from the command line, which is
  270.    null-terminated */ 
  271. char *prog_start;
  272. char *prog_end;
  273.  
  274. /* When we're reading a script command from a string, 'prog_cur' points
  275.    to the current character in the string */
  276. char *prog_cur;
  277.  
  278. /* This is the name of the current script file.
  279.    It is used for error messages. */
  280. char *prog_name;
  281.  
  282. /* This is the current script file.  If it is zero, we are reading
  283.    from a string stored in 'prog_start' instead.  If both 'prog_file'
  284.    and 'prog_start' are zero, we're in trouble! */
  285. FILE *prog_file;
  286.  
  287. /* this is the number of the current script line that we're compiling.  It is
  288.    used to give out useful and informative error messages. */
  289. int prog_line = 1;
  290.  
  291. /* This is the file pointer that we're currently reading data from.  It may
  292.    be stdin */
  293. FILE *input_file;
  294.  
  295. /* If this variable is non-zero at exit, one or more of the input
  296.    files couldn't be opened. */
  297.  
  298. int bad_input = 0;
  299.  
  300. /* 'an empty regular expression is equivalent to the last regular
  301.    expression read' so we have to keep track of the last regex used.
  302.    Here's where we store a pointer to it (it is only malloc()'d once) */
  303. struct re_pattern_buffer *last_regex;
  304.  
  305. /* Various error messages we may want to print */
  306. static char ONE_ADDR[] = "Command only uses one address";
  307. static char NO_ADDR[] = "Command doesn't take any addresses";
  308. static char LINE_JUNK[] = "Extra characters after command";
  309. static char BAD_EOF[] = "Unexpected End-of-file";
  310. static char NO_REGEX[] = "No previous regular expression";
  311.  
  312. static struct option longopts[] =
  313. {
  314.   {"expression", 1, NULL, 'e'},
  315.   {"file", 1, NULL, 'f'},
  316.   {"quiet", 0, NULL, 'n'},
  317.   {"silent", 0, NULL, 'n'},
  318.   {"version", 0, NULL, 'V'},
  319.   {NULL, 0, NULL, 0}
  320. };
  321.  
  322. /* Yes, the main program, which parses arguments, and does the right
  323.    thing with them; it also inits the temporary storage, etc. */
  324. void
  325. main(argc,argv)
  326. int argc;
  327. char **argv;
  328. {
  329.     int opt;
  330.     char *e_strings = NULL;
  331.     int compiled = 0;
  332.     struct sed_label *go,*lbl;
  333.  
  334.     myname=argv[0];
  335.     while((opt=getopt_long(argc,argv,"ne:f:V", longopts, (int *) 0))
  336.           !=EOF) {
  337.         switch(opt) {
  338.         case 'n':
  339.             no_default_output = 1;
  340.             break;
  341.         case 'e':
  342.             if(e_strings == NULL) {
  343.                 e_strings=ck_malloc(strlen(optarg)+2);
  344.                 strcpy(e_strings,optarg);
  345.             } else {
  346.                 e_strings=ck_realloc(e_strings,strlen(e_strings)+strlen(optarg)+2);
  347.                 strcat(e_strings,optarg);
  348.             }
  349.             strcat(e_strings,"\n");
  350.             compiled = 1;
  351.             break;
  352.         case 'f':
  353.             compile_file(optarg);
  354.             compiled = 1;
  355.             break;
  356.         case 'V':
  357.             fprintf(stderr, "%s\n", version_string);
  358.             break;
  359.         default:
  360.             usage();
  361.         }
  362.     }
  363.     if(e_strings) {
  364.         compile_string(e_strings);
  365.         free(e_strings);
  366.     }
  367.     if(!compiled) {
  368.         if (optind == argc)
  369.             usage();
  370.         compile_string(argv[optind++]);
  371.     }
  372.  
  373.     for(go=jumps;go;go=go->next) {
  374.         for(lbl=labels;lbl;lbl=lbl->next)
  375.             if(!strcmp(lbl->name,go->name))
  376.                 break;
  377.         if(*go->name && !lbl)
  378.             panic("Can't find label for jump to '%s'",go->name);
  379.         go->v->v[go->v_index].x.jump=lbl;
  380.     }
  381.  
  382.     line.length=0;
  383.     line.alloc=50;
  384.     line.text=ck_malloc(50);
  385.  
  386.     append.length=0;
  387.     append.alloc=50;
  388.     append.text=ck_malloc(50);
  389.  
  390.     hold.length=0;
  391.     hold.alloc=50;
  392.     hold.text=ck_malloc(50);
  393.  
  394.     if(argc<=optind) {
  395.         last_input_file++;
  396.         read_file("-");
  397.     } else while(optind<argc) {
  398.         if(optind==argc-1)
  399.             last_input_file++;
  400.         read_file(argv[optind]);
  401.         optind++;
  402.         if(quit_cmd)
  403.             break;
  404.     }
  405.     if(bad_input)
  406.         exit(2);
  407.     exit(0);
  408. }
  409.  
  410. /* 'str' is a string (from the command line) that contains a sed command.
  411.    Compile the command, and add it to the end of 'the_program' */
  412. void
  413. compile_string(str)
  414. char *str;
  415. {
  416.     prog_file = 0;
  417.     prog_line=0;
  418.     prog_start=prog_cur=str;
  419.     prog_end=str+strlen(str);
  420.     the_program=compile_program(the_program);
  421. }
  422.  
  423. /* 'str' is the name of a file containing sed commands.  Read them in
  424.    and add them to the end of 'the_program' */
  425. void
  426. compile_file(str)
  427. char *str;
  428. {
  429.     int ch;
  430.  
  431.     prog_start=prog_cur=prog_end=0;
  432.     prog_name=str;
  433.     prog_line=1;
  434.     if(str[0]=='-' && str[1]=='\0')
  435.         prog_file=stdin;
  436.     else
  437.         prog_file=ck_fopen(str,"r");
  438.     ch=getc(prog_file);
  439.     if(ch=='#') {
  440.         ch=getc(prog_file);
  441.         if(ch=='n')
  442.             no_default_output++;
  443.         while(ch!=EOF && ch!='\n')
  444.             ch=getc(prog_file);
  445.     } else if(ch!=EOF)
  446.         ungetc(ch,prog_file);
  447.     the_program=compile_program(the_program);
  448. }
  449.  
  450. #define MORE_CMDS 40
  451.  
  452. /* Read a program (or a subprogram within '{' '}' pairs) in and store
  453.    the compiled form in *'vector'  Return a pointer to the new vector.  */
  454. struct vector *
  455. compile_program(vector)
  456. struct vector *vector;
  457. {
  458.     struct sed_cmd *cur_cmd;
  459.     int    ch;
  460.     int    slash;
  461.     VOID    *b;
  462.     unsigned char    *string;
  463.     int    num;
  464.  
  465.     if(!vector) {
  466.         vector=(struct vector *)ck_malloc(sizeof(struct vector));
  467.         vector->v=(struct sed_cmd *)ck_malloc(MORE_CMDS*sizeof(struct sed_cmd));
  468.         vector->v_allocated=MORE_CMDS;
  469.         vector->v_length=0;
  470.         vector->up_one = 0;
  471.         vector->next_one = 0;
  472.     }
  473.     for(;;) {
  474.     skip_comment:
  475.         do ch=inchar();
  476.         while(ch!=EOF && (isblank(ch) || ch=='\n' || ch==';'));
  477.         if(ch==EOF)
  478.             break;
  479.         savchar(ch);
  480.  
  481.         if(vector->v_length==vector->v_allocated) {
  482.             vector->v=(struct sed_cmd *)ck_realloc((VOID *)vector->v,(vector->v_length+MORE_CMDS)*sizeof(struct sed_cmd));
  483.             vector->v_allocated+=MORE_CMDS;
  484.         }
  485.         cur_cmd=vector->v+vector->v_length;
  486.         vector->v_length++;
  487.  
  488.         cur_cmd->a1.addr_type=0;
  489.         cur_cmd->a2.addr_type=0;
  490.         cur_cmd->aflags=0;
  491.         cur_cmd->cmd=0;
  492.  
  493.         if(compile_address(&(cur_cmd->a1))) {
  494.             ch=inchar();
  495.             if(ch==',') {
  496.                 do ch=inchar();
  497.                 while(ch!=EOF && isblank(ch));
  498.                 savchar(ch);
  499.                 if(compile_address(&(cur_cmd->a2)))
  500.                     ;
  501.                 else
  502.                     bad_prog("Unexpected ','");
  503.             } else
  504.                 savchar(ch);
  505.         }
  506.         ch=inchar();
  507.         if(ch==EOF)
  508.             break;
  509.  new_cmd:
  510.         switch(ch) {
  511.         case '#':
  512.             if(cur_cmd->a1.addr_type!=0)
  513.                 bad_prog(NO_ADDR);
  514.             do ch=inchar();
  515.             while(ch!=EOF && ch!='\n');
  516.             vector->v_length--;
  517.             goto skip_comment;
  518.         case '!':
  519.             if(cur_cmd->aflags & ADDR_BANG_BIT)
  520.                 bad_prog("Multiple '!'s");
  521.             cur_cmd->aflags|= ADDR_BANG_BIT;
  522.             do ch=inchar();
  523.             while(ch!=EOF && isblank(ch));
  524.             if(ch==EOF)
  525.                 bad_prog(BAD_EOF);
  526. #if 0
  527.             savchar(ch);
  528. #endif
  529.             goto new_cmd;
  530.         case 'a':
  531.         case 'i':
  532.             if(cur_cmd->a2.addr_type!=0)
  533.                 bad_prog(ONE_ADDR);
  534.             /* Fall Through */
  535.         case 'c':
  536.             cur_cmd->cmd=ch;
  537.             if(inchar()!='\\' || inchar()!='\n')
  538.                 bad_prog(LINE_JUNK);
  539.             b=init_buffer();
  540.             while((ch=inchar())!=EOF && ch!='\n') {
  541.                 if(ch=='\\')
  542.                     ch=inchar();
  543.                 add1_buffer(b,ch);
  544.             }
  545.             if(ch!=EOF)
  546.                 add1_buffer(b,ch);
  547.             num=size_buffer(b);
  548.             string=(unsigned char *)ck_malloc(num);
  549.             bcopy(get_buffer(b),string,num);
  550.             flush_buffer(b);
  551.             cur_cmd->x.cmd_txt.text_len=num;
  552.             cur_cmd->x.cmd_txt.text=(char *)string;
  553.             break;
  554.         case '{':
  555.             cur_cmd->cmd=ch;
  556.             program_depth++;
  557. #if 0
  558.             while((ch=inchar())!=EOF && ch!='\n')
  559.                 if(!isblank(ch))
  560.                     bad_prog(LINE_JUNK);
  561. #endif
  562.             cur_cmd->x.sub=compile_program((struct vector *)0);
  563.             /* FOO JF is this the right thing to do? */
  564.             break;
  565.         case '}':
  566.             if(!program_depth)
  567.                 bad_prog("Unexpected '}'");
  568.             --(vector->v_length);
  569.             while((ch=inchar())!=EOF && ch!='\n' && ch!=';')
  570.                 if(!isblank(ch))
  571.                     bad_prog(LINE_JUNK);
  572.             return vector;
  573.         case ':':
  574.             cur_cmd->cmd=ch;
  575.             if(cur_cmd->a1.addr_type!=0)
  576.                 bad_prog(": doesn't want any addresses");
  577.             labels=setup_jump(labels,cur_cmd,vector);
  578.             break;
  579.         case 'b':
  580.         case 't':
  581.             cur_cmd->cmd=ch;
  582.             jumps=setup_jump(jumps,cur_cmd,vector);
  583.             break;
  584.         case 'q':
  585.         case '=':
  586.             if(cur_cmd->a2.addr_type)
  587.                 bad_prog(ONE_ADDR);
  588.             /* Fall Through */
  589.         case 'd':
  590.         case 'D':
  591.         case 'g':
  592.         case 'G':
  593.         case 'h':
  594.         case 'H':
  595.         case 'l':
  596.         case 'n':
  597.         case 'N':
  598.         case 'p':
  599.         case 'P':
  600.         case 'x':
  601.             cur_cmd->cmd=ch;
  602.             do    ch=inchar();
  603.             while(ch!=EOF && isblank(ch) && ch!='\n' && ch!=';');
  604.             if(ch!='\n' && ch!=';' && ch!=EOF)
  605.                 bad_prog(LINE_JUNK);
  606.             break;
  607.  
  608.         case 'r':
  609.             if(cur_cmd->a2.addr_type!=0)
  610.                 bad_prog(ONE_ADDR);
  611.             /* FALL THROUGH */
  612.         case 'w':
  613.             cur_cmd->cmd=ch;
  614.             cur_cmd->x.io_file=compile_filename(ch=='r');
  615.             break;
  616.  
  617.         case 's':
  618.             cur_cmd->cmd=ch;
  619.             slash=inchar();
  620.             compile_regex(slash);
  621.  
  622.             cur_cmd->x.cmd_regex.regx=last_regex;
  623.  
  624.             b=init_buffer();
  625.             while((ch=inchar())!=EOF && ch!=slash) {
  626.                 if(ch=='\\') {
  627.                     int ci;
  628.  
  629.                     ci=inchar();
  630.                     if(ci!=EOF) {
  631.                         if(ci!='\n')
  632.                             add1_buffer(b,ch);
  633.                         add1_buffer(b,ci);
  634.                     }
  635.                 } else
  636.                     add1_buffer(b,ch);
  637.             }
  638.             cur_cmd->x.cmd_regex.replace_length=size_buffer(b);
  639.             cur_cmd->x.cmd_regex.replacement=ck_malloc(cur_cmd->x.cmd_regex.replace_length);
  640.             bcopy(get_buffer(b),cur_cmd->x.cmd_regex.replacement,cur_cmd->x.cmd_regex.replace_length);
  641.             flush_buffer(b);
  642.  
  643.             cur_cmd->x.cmd_regex.flags=0;
  644.             cur_cmd->x.cmd_regex.numb=0;
  645.  
  646.             if(ch==EOF)
  647.                 break;
  648.             do {
  649.                 ch=inchar();
  650.                 switch(ch) {
  651.                 case 'p':
  652.                     if(cur_cmd->x.cmd_regex.flags&S_PRINT_BIT)
  653.                         bad_prog("multiple 'p' options to 's' command");
  654.                     cur_cmd->x.cmd_regex.flags|=S_PRINT_BIT;
  655.                     break;
  656.                 case 'g':
  657.                     if(cur_cmd->x.cmd_regex.flags&S_NUM_BIT)
  658.                         cur_cmd->x.cmd_regex.flags&= ~S_NUM_BIT;
  659.                     if(cur_cmd->x.cmd_regex.flags&S_GLOBAL_BIT)
  660.                         bad_prog("multiple 'g' options to 's' command");
  661.                     cur_cmd->x.cmd_regex.flags|=S_GLOBAL_BIT;
  662.                     break;
  663.                 case 'w':
  664.                     cur_cmd->x.cmd_regex.flags|=S_WRITE_BIT;
  665.                     cur_cmd->x.cmd_regex.wio_file=compile_filename(0);
  666.                     ch='\n';
  667.                     break;
  668.                 case '0': case '1': case '2': case '3':
  669.                 case '4': case '5': case '6': case '7':
  670.                 case '8': case '9':
  671.                     if(cur_cmd->x.cmd_regex.flags&S_NUM_BIT)
  672.                         bad_prog("multiple number options to 's' command");
  673.                     if((cur_cmd->x.cmd_regex.flags&S_GLOBAL_BIT)==0)
  674.                         cur_cmd->x.cmd_regex.flags|=S_NUM_BIT;
  675.                     num = 0;
  676.                     while(isdigit(ch)) {
  677.                         num=num*10+ch-'0';
  678.                         ch=inchar();
  679.                     }
  680.                     savchar(ch);
  681.                     cur_cmd->x.cmd_regex.numb=num;
  682.                     break;
  683.                 case '\n':
  684.                 case ';':
  685.                 case EOF:
  686.                     break;
  687.                 default:
  688.                     bad_prog("Unknown option to 's'");
  689.                     break;
  690.                 }
  691.             } while(ch!=EOF && ch!='\n' && ch!=';');
  692.             if(ch==EOF)
  693.                 break;
  694.             break;
  695.  
  696.         case 'y':
  697.             cur_cmd->cmd=ch;
  698.             string=(unsigned char *)ck_malloc(256);
  699.             for(num=0;num<256;num++)
  700.                 string[num]=num;
  701.             b=init_buffer();
  702.             slash=inchar();
  703.             while((ch=inchar())!=EOF && ch!=slash)
  704.                 add1_buffer(b,ch);
  705.             cur_cmd->x.translate=string;
  706.             string=(unsigned char *)get_buffer(b);
  707.             for(num=size_buffer(b);num;--num) {
  708.                 ch=inchar();
  709.                 if(ch==EOF)
  710.                     bad_prog(BAD_EOF);
  711.                 if(ch==slash)
  712.                     bad_prog("strings for y command are different lengths");
  713.                 cur_cmd->x.translate[*string++]=ch;
  714.             }
  715.             flush_buffer(b);
  716.             if(inchar()!=slash || ((ch=inchar())!=EOF && ch!='\n' && ch!=';'))
  717.                 bad_prog(LINE_JUNK);
  718.             break;
  719.  
  720.         default:
  721.             bad_prog("Unknown command");
  722.         }
  723.     }
  724.     return vector;
  725. }
  726.  
  727. /* Complain about a programming error and exit. */
  728. void
  729. bad_prog(why)
  730. char *why;
  731. {
  732.     if(prog_line)
  733.         fprintf(stderr,"%s: file %s line %d: %s\n",myname,prog_name,prog_line,why);
  734.     else
  735.         fprintf(stderr,"%s: %s\n",myname,why);
  736.     exit(1);
  737. }
  738.  
  739. /* Read the next character from the program.  Return EOF if there isn't
  740.    anything to read.  Keep prog_line up to date, so error messages can
  741.    be meaningful. */
  742. int
  743. inchar()
  744. {
  745.     int    ch;
  746.     if(prog_file) {
  747.         if(feof(prog_file))
  748.             return EOF;
  749.         else
  750.             ch=getc(prog_file);
  751.     } else {
  752.         if(!prog_cur)
  753.             return EOF;
  754.         else if(prog_cur==prog_end) {
  755.             ch=EOF;
  756.             prog_cur=0;
  757.         } else
  758.             ch= *prog_cur++;
  759.     }
  760.     if(ch=='\n' && prog_line)
  761.         prog_line++;
  762.     return ch;
  763. }
  764.  
  765. /* unget 'ch' so the next call to inchar will return it.  'ch' must not be
  766.    EOF or anything nasty like that. */
  767. void
  768. savchar(ch)
  769. int ch;
  770. {
  771.     if(ch==EOF)
  772.         return;
  773.     if(ch=='\n' && prog_line>1)
  774.         --prog_line;
  775.     if(prog_file)
  776.         ungetc(ch,prog_file);
  777.     else
  778.         *--prog_cur=ch;
  779. }
  780.  
  781.  
  782. /* Try to read an address for a sed command.  If it succeeeds,
  783.    return non-zero and store the resulting address in *'addr'.
  784.    If the input doesn't look like an address read nothing
  785.    and return zero. */
  786. int
  787. compile_address(addr)
  788. struct addr *addr;
  789. {
  790.     int    ch;
  791.     int    num;
  792.  
  793.     ch=inchar();
  794.  
  795.     if(isdigit(ch)) {
  796.         num=ch-'0';
  797.         while((ch=inchar())!=EOF && isdigit(ch))
  798.             num=num*10+ch-'0';
  799.         while(ch!=EOF && isblank(ch))
  800.             ch=inchar();
  801.         savchar(ch);
  802.         addr->addr_type=ADDR_NUM;
  803.         addr->addr_number = num;
  804.         return 1;
  805.     } else if(ch=='/') {
  806.         addr->addr_type=ADDR_REGEX;
  807.         compile_regex('/');
  808.         addr->addr_regex=last_regex;
  809.         do ch=inchar();
  810.         while(ch!=EOF && isblank(ch));
  811.         savchar(ch);
  812.         return 1;
  813.     } else if(ch=='$') {
  814.         addr->addr_type=ADDR_LAST;
  815.         do ch=inchar();
  816.         while(ch!=EOF && isblank(ch));
  817.         savchar(ch);
  818.         return 1;
  819.     } else
  820.         savchar(ch);
  821.     return 0;
  822. }
  823.  
  824. void
  825. compile_regex (slash)
  826.      int slash;
  827. {
  828.     VOID *b;
  829.     int ch;
  830.     int in_char_class = 0;
  831.  
  832.     b=init_buffer();
  833.     while((ch=inchar())!=EOF && (ch!=slash || in_char_class)) {
  834.         if(ch=='^') {
  835.             if(size_buffer(b)==0) {
  836.                 add1_buffer(b,'\\');
  837.                 add1_buffer(b,'`');
  838.             } else
  839.                 add1_buffer(b,ch);
  840.             continue;
  841.         } else if(ch=='$') {
  842.             ch=inchar();
  843.             savchar(ch);
  844.             if(ch==slash) {
  845.                 add1_buffer(b,'\\');
  846.                 add1_buffer(b,'\'');
  847.             } else
  848.                 add1_buffer(b,'$');
  849.             continue;
  850.         } else if(ch == '[') {
  851.             add1_buffer(b,ch);
  852.             in_char_class = 1;
  853.             continue;
  854.         } else if(ch == ']') {
  855.             add1_buffer(b,ch);
  856.             in_char_class = 0;
  857.             continue;
  858.         } else if(ch!='\\') {
  859.             add1_buffer(b,ch);
  860.             continue;
  861.         }
  862.         ch=inchar();
  863.         switch(ch) {
  864.         case 'n':
  865.             add1_buffer(b,'\n');
  866.             break;
  867. #if 0
  868.         case 'b':
  869.             add1_buffer(b,'\b');
  870.             break;
  871.         case 'f':
  872.             add1_buffer(b,'\f');
  873.             break;
  874.         case 'r':
  875.             add1_buffer(b,'\r');
  876.             break;
  877.         case 't':
  878.             add1_buffer(b,'\t');
  879.             break;
  880. #endif /* 0 */
  881.         case EOF:
  882.             break;
  883.         default:
  884.             add1_buffer(b,'\\');
  885.             add1_buffer(b,ch);
  886.             break;
  887.         }
  888.     }
  889.     if(ch==EOF)
  890.         bad_prog(BAD_EOF);
  891.     if(size_buffer(b)) {
  892.         last_regex=(struct re_pattern_buffer *)ck_malloc(sizeof(struct re_pattern_buffer));
  893.         last_regex->allocated=size_buffer(b)+10;
  894.         last_regex->buffer=ck_malloc(last_regex->allocated);
  895.         last_regex->fastmap=ck_malloc(256);
  896.         last_regex->translate=0;
  897.         re_compile_pattern(get_buffer(b),size_buffer(b),last_regex);
  898.     } else if(!last_regex)
  899.         bad_prog(NO_REGEX);
  900.     flush_buffer(b);
  901. }
  902.  
  903. /* Store a label (or label reference) created by a ':', 'b', or 't'
  904.    comand so that the jump to/from the lable can be backpatched after
  905.    compilation is complete */
  906. struct sed_label *
  907. setup_jump(list,cmd,vec)
  908. struct sed_label *list;
  909. struct sed_cmd *cmd;
  910. struct vector *vec;
  911. {
  912.     struct sed_label *tmp;
  913.     VOID *b;
  914.     int ch;
  915.  
  916.     b=init_buffer();
  917.     while((ch=inchar()) != EOF && isblank(ch))
  918.         ;
  919.     while(ch!=EOF && ch!='\n') {
  920.         add1_buffer(b,ch);
  921.         ch=inchar();
  922.     }
  923.     savchar(ch);
  924.     add1_buffer(b,'\0');
  925.     tmp=(struct sed_label *)ck_malloc(sizeof(struct sed_label));
  926.     tmp->v=vec;
  927.     tmp->v_index=cmd-vec->v;
  928.     tmp->name=ck_strdup(get_buffer(b));
  929.     tmp->next=list;
  930.     flush_buffer(b);
  931.     return tmp;
  932. }
  933.  
  934. /* read in a filename for a 'r', 'w', or 's///w' command, and
  935.    update the internal structure about files.  The file is
  936.    opened if it isn't already open. */
  937. FILE *
  938. compile_filename(readit)
  939.      int readit;
  940. {
  941.     char *file_name;
  942.     int n;
  943.     VOID *b;
  944.     int ch;
  945.  
  946.     if(inchar()!=' ')
  947.         bad_prog("missing ' ' before filename");
  948.     b=init_buffer();
  949.     while((ch=inchar())!=EOF && ch!='\n')
  950.         add1_buffer(b,ch);
  951.     add1_buffer(b,'\0');
  952.     file_name=get_buffer(b);
  953.     for(n=0;n<NUM_FPS;n++) {
  954.         if(!file_ptrs[n].name)
  955.             break;
  956.         if(!strcmp(file_ptrs[n].name,file_name)) {
  957.             if(file_ptrs[n].readit!=readit)
  958.                 bad_prog("Can't open file for both reading and writing");
  959.             flush_buffer(b);
  960.             return file_ptrs[n].phile;
  961.         }
  962.     }
  963.     if(n<NUM_FPS) {
  964.         file_ptrs[n].name=ck_strdup(file_name);
  965.         file_ptrs[n].readit=readit;
  966.         if (!readit)
  967.             file_ptrs[n].phile=ck_fopen(file_name,"a");
  968.         else if (access(file_name, 4) == 0)
  969.             file_ptrs[n].phile=ck_fopen(file_name,"r");
  970.         else if (access(file_name, 4) == 0)
  971.             file_ptrs[n].phile=ck_fopen(file_name,"r");
  972.         else
  973.             file_ptrs[n].phile=ck_fopen("/dev/null", "r");
  974.         flush_buffer(b);
  975.         return file_ptrs[n].phile;
  976.     } else {
  977.         bad_prog("Hopelessely evil compiled in limit on number of open files.  re-compile sed");
  978.         return 0;
  979.     }
  980. }
  981.  
  982. /* Parse a filename given by a 'r' 'w' or 's///w' command. */
  983. void
  984. read_file(name)
  985. char *name;
  986. {
  987.     if(*name=='-' && name[1]=='\0')
  988.         input_file=stdin;
  989.     else {
  990.         input_file=fopen(name,"r");
  991.         if(input_file==0) {
  992.             extern int errno;
  993.             extern char *sys_errlist[];
  994.             extern int sys_nerr;
  995.  
  996.             char *ptr;
  997.  
  998.             ptr=(errno>=0 && errno<sys_nerr) ? sys_errlist[errno] : "Unknown error code";
  999.             bad_input++;
  1000.             fprintf(stderr,"%s: can't read %s: %s\n",myname,name,ptr);
  1001.  
  1002.             return;
  1003.         }
  1004.     }
  1005.     while(read_pattern_space()) {
  1006.         execute_program(the_program);
  1007.         if(!no_default_output)
  1008.             ck_fwrite(line.text,1,line.length,stdout);
  1009.         if(append.length) {
  1010.             ck_fwrite(append.text,1,append.length,stdout);
  1011.             append.length=0;
  1012.         }
  1013.         if(quit_cmd)
  1014.             break;
  1015.     }
  1016.     ck_fclose(input_file);
  1017. }
  1018.  
  1019. /* Execute the program 'vec' on the current input line. */
  1020. void
  1021. execute_program(vec)
  1022. struct vector *vec;
  1023. {
  1024.     struct sed_cmd *cur_cmd;
  1025.     int    n;
  1026.     int addr_matched;
  1027.     static int end_cycle;
  1028.  
  1029.     int start;
  1030.     int remain;
  1031.     int offset;
  1032.  
  1033.     static struct line tmp;
  1034.     struct line t;
  1035.     char *rep,*rep_end,*rep_next,*rep_cur;
  1036.  
  1037.     struct re_registers regs;
  1038.     int count = 0;
  1039.  
  1040.     end_cycle = 0;
  1041.  
  1042.     for(cur_cmd=vec->v,n=vec->v_length;n;cur_cmd++,n--) {
  1043.  
  1044.     exe_loop:
  1045.         addr_matched=0;
  1046.         if(cur_cmd->aflags&A1_MATCHED_BIT) {
  1047.             addr_matched=1;
  1048.             if(match_address(&(cur_cmd->a2)))
  1049.                 cur_cmd->aflags&=~A1_MATCHED_BIT;
  1050.         } else if(match_address(&(cur_cmd->a1))) {
  1051.             addr_matched=1;
  1052.             if(cur_cmd->a2.addr_type!=ADDR_NULL)
  1053.                 cur_cmd->aflags|=A1_MATCHED_BIT;
  1054.         }
  1055.         if(cur_cmd->aflags&ADDR_BANG_BIT)
  1056.             addr_matched= !addr_matched;
  1057.         if(!addr_matched)
  1058.             continue;
  1059.         switch(cur_cmd->cmd) {
  1060.         case '{':    /* Execute sub-program */
  1061.             execute_program(cur_cmd->x.sub);
  1062.             break;
  1063.  
  1064.         case ':':    /* Executing labels is easy. */
  1065.             break;
  1066.  
  1067.         case '=':
  1068.             printf("%d\n",input_line_number);
  1069.             break;
  1070.  
  1071.         case 'a':
  1072.             while(append.alloc-append.length<cur_cmd->x.cmd_txt.text_len) {
  1073.                 append.alloc *= 2;
  1074.                 append.text=ck_realloc(append.text,append.alloc);
  1075.             }
  1076.             bcopy(cur_cmd->x.cmd_txt.text,append.text+append.length,cur_cmd->x.cmd_txt.text_len);
  1077.             append.length+=cur_cmd->x.cmd_txt.text_len;
  1078.             break;
  1079.  
  1080.         case 'b':
  1081.             if(!cur_cmd->x.jump)
  1082.                 end_cycle++;
  1083.             else {
  1084.                 struct sed_label *j = cur_cmd->x.jump;
  1085.  
  1086.                 n= j->v->v_length - j->v_index;
  1087.                 cur_cmd= j->v->v + j->v_index;
  1088.                 goto exe_loop;
  1089.             }
  1090.             break;
  1091.  
  1092.         case 'c':
  1093.             line.length=0;
  1094.             if(!(cur_cmd->aflags&A1_MATCHED_BIT))
  1095.                 ck_fwrite(cur_cmd->x.cmd_txt.text,1,cur_cmd->x.cmd_txt.text_len,stdout);
  1096.             end_cycle++;
  1097.             break;
  1098.  
  1099.         case 'd':
  1100.             line.length=0;
  1101.             end_cycle++;
  1102.             break;
  1103.  
  1104.         case 'D':
  1105.             {
  1106.                 char *tmp;
  1107.                 int newlength;
  1108.  
  1109.                 tmp=memchr(line.text,'\n',line.length);
  1110.                 newlength=line.length-(tmp-line.text);
  1111.                 if(newlength)
  1112.                     memmove(line.text,tmp,newlength);
  1113.                 line.length=newlength;
  1114.             }
  1115.             end_cycle++;
  1116.             break;
  1117.  
  1118.         case 'g':
  1119.             line_copy(&hold,&line);
  1120.             break;
  1121.  
  1122.         case 'G':
  1123.             line_append(&hold,&line);
  1124.             break;
  1125.  
  1126.         case 'h':
  1127.             line_copy(&line,&hold);
  1128.             break;
  1129.  
  1130.         case 'H':
  1131.             line_append(&line,&hold);
  1132.             break;
  1133.  
  1134.         case 'i':
  1135.             ck_fwrite(cur_cmd->x.cmd_txt.text,1,cur_cmd->x.cmd_txt.text_len,stdout);
  1136.             break;
  1137.  
  1138.         case 'l':
  1139.             {
  1140.                 char *tmp;
  1141.                 int n;
  1142.                 int width = 0;
  1143.  
  1144.                 n=line.length;
  1145.                 tmp=line.text;
  1146.                 /* Use --n so this'll skip the trailing newline */
  1147.                 while(--n) {
  1148.                     if(width>77) {
  1149.                         width=0;
  1150.                         putchar('\n');
  1151.                     }
  1152.                     if(*tmp == '\\') {
  1153.                             printf("\\\\");
  1154.                         width+=2;
  1155.                     } else if(isprint(*tmp)) {
  1156.                         putchar(*tmp);
  1157.                         width++;
  1158.                     } else switch(*tmp) {
  1159. #if 0
  1160.   /* Should print \00 instead of \0 because (a) POSIX requires it, and
  1161.      (b) this way \01 is unambiguous.  */
  1162.                     case '\0':
  1163.                         printf("\\0");
  1164.                         width+=2;
  1165.                         break;
  1166. #endif
  1167.                     case 007:
  1168.                         printf("\\a");
  1169.                         width+=2;
  1170.                         break;
  1171.                     case '\b':
  1172.                         printf("\\b");
  1173.                         width+=2;
  1174.                         break;
  1175.                     case '\f':
  1176.                         printf("\\f");
  1177.                         width+=2;
  1178.                         break;
  1179.                     case '\n':
  1180.                         printf("\\n");
  1181.                         width+=2;
  1182.                         break;
  1183.                     case '\r':
  1184.                         printf("\\r");
  1185.                         width+=2;
  1186.                         break;
  1187.                     case '\t':
  1188.                         printf("\\t");
  1189.                         width+=2;
  1190.                         break;
  1191.                     case '\v':
  1192.                         printf("\\v");
  1193.                         width+=2;
  1194.                         break;
  1195.                     default:
  1196.                         printf("\\%02x",(*tmp)&0xFF);
  1197.                         width+=2;
  1198.                         break;
  1199.                     }
  1200.                     tmp++;
  1201.                 }
  1202.                 putchar('\n');
  1203.             }
  1204.             break;
  1205.  
  1206.         case 'n':
  1207.             if (feof(input_file)) goto quit;
  1208.             ck_fwrite(line.text,1,line.length,stdout);
  1209.             read_pattern_space();
  1210.             break;
  1211.  
  1212.         case 'N':
  1213.             if (feof(input_file)) goto quit;
  1214.             append_pattern_space();
  1215.             break;
  1216.  
  1217.         case 'p':
  1218.             ck_fwrite(line.text,1,line.length,stdout);
  1219.             break;
  1220.  
  1221.         case 'P':
  1222.             {
  1223.                 char *tmp;
  1224.  
  1225.                 tmp=memchr(line.text,'\n',line.length);
  1226.                 ck_fwrite(line.text, 1,
  1227.                       tmp ? tmp - line.text + 1
  1228.                       : line.length, stdout);
  1229.             }
  1230.             break;
  1231.  
  1232.         case 'q': quit:
  1233.             quit_cmd++;
  1234.             end_cycle++;
  1235.             break;
  1236.  
  1237.         case 'r':
  1238.             {
  1239.                 int n = 0;
  1240.  
  1241.                 rewind(cur_cmd->x.io_file);
  1242.                 do {
  1243.                     append.length += n;
  1244.                     if(append.length==append.alloc) {
  1245.                         append.alloc *= 2;
  1246.                         append.text = ck_realloc(append.text, append.alloc);
  1247.                     }
  1248.                 } while((n=fread(append.text+append.length,sizeof(char),append.alloc-append.length,cur_cmd->x.io_file))>0);
  1249.                 if(ferror(cur_cmd->x.io_file))
  1250.                     panic("Read error on input file to 'r' command");
  1251.             }
  1252.             break;
  1253.  
  1254.         case 's':
  1255.             if(!tmp.alloc) {
  1256.                 tmp.alloc=50;
  1257.                 tmp.text=ck_malloc(50);
  1258.             }
  1259.             count=0;
  1260.             start = 0;
  1261.             remain=line.length-1;
  1262.             tmp.length=0;
  1263.             rep = cur_cmd->x.cmd_regex.replacement;
  1264.             rep_end=rep+cur_cmd->x.cmd_regex.replace_length;
  1265.  
  1266.             while((offset = re_search(cur_cmd->x.cmd_regex.regx,
  1267.                           line.text,
  1268.                           line.length-1,
  1269.                           start,
  1270.                           remain,
  1271.                           ®s))>=0) {
  1272.                 count++;
  1273.                 if(offset-start)
  1274.                     str_append(&tmp,line.text+start,offset-start);
  1275.  
  1276.                 if(cur_cmd->x.cmd_regex.flags&S_NUM_BIT) {
  1277.                     if(count!=cur_cmd->x.cmd_regex.numb) {
  1278.                         str_append(&tmp,line.text+regs.start[0],regs.end[0]-regs.start[0]);
  1279.                         start = (offset == regs.end[0] ? offset + 1 : regs.end[0]);
  1280.                         remain = (line.length-1) - start;
  1281.                         continue;
  1282.                     }
  1283.                 }
  1284.  
  1285.                 for(rep_next=rep_cur=rep;rep_next<rep_end;rep_next++) {
  1286.                     if(*rep_next=='&') {
  1287.                         if(rep_next-rep_cur)
  1288.                             str_append(&tmp,rep_cur,rep_next-rep_cur);
  1289.                         str_append(&tmp,line.text+regs.start[0],regs.end[0]-regs.start[0]);
  1290.                         rep_cur=rep_next+1;
  1291.                     } else if(*rep_next=='\\') {
  1292.                         if(rep_next-rep_cur)
  1293.                             str_append(&tmp,rep_cur,rep_next-rep_cur);
  1294.                         rep_next++;
  1295.                         if(rep_next!=rep_end) {
  1296.                             int n;
  1297.  
  1298.                             if(*rep_next>='0' && *rep_next<='9') {
  1299.                                 n= *rep_next -'0';
  1300.                                 str_append(&tmp,line.text+regs.start[n],regs.end[n]-regs.start[n]);
  1301.                             } else
  1302.                                 str_append(&tmp,rep_next,1);
  1303.                         }
  1304.                         rep_cur=rep_next+1;
  1305.                     }
  1306.                 }
  1307.                 if(rep_next-rep_cur)
  1308.                     str_append(&tmp,rep_cur,rep_next-rep_cur);
  1309.                 if (offset == regs.end[0]) {
  1310.                     str_append(&tmp, line.text + offset, 1);
  1311.                     ++regs.end[0];
  1312.                 }
  1313.                 start = regs.end[0];
  1314.  
  1315.                 remain = (line.length-1) - start;
  1316.                 if(remain<0)
  1317.                     break;
  1318.                 if(!(cur_cmd->x.cmd_regex.flags&S_GLOBAL_BIT))
  1319.                     break;
  1320.             }
  1321.             if(!count)
  1322.                 break;
  1323.             replaced=1;
  1324.             str_append(&tmp,line.text+start,remain+1);
  1325.             t.text=line.text;
  1326.             t.length=line.length;
  1327.             t.alloc=line.alloc;
  1328.             line.text=tmp.text;
  1329.             line.length=tmp.length;
  1330.             line.alloc=tmp.alloc;
  1331.             tmp.text=t.text;
  1332.             tmp.length=t.length;
  1333.             tmp.alloc=t.alloc;
  1334.             if(cur_cmd->x.cmd_regex.flags&S_WRITE_BIT)
  1335.                 ck_fwrite(line.text,1,line.length,cur_cmd->x.cmd_regex.wio_file);
  1336.             if(cur_cmd->x.cmd_regex.flags&S_PRINT_BIT)
  1337.                 ck_fwrite(line.text,1,line.length,stdout);
  1338.             break;
  1339.  
  1340.         case 't':
  1341.             if(replaced) {
  1342.                 replaced = 0;
  1343.                 if(!cur_cmd->x.jump)
  1344.                     end_cycle++;
  1345.                 else {
  1346.                     struct sed_label *j = cur_cmd->x.jump;
  1347.  
  1348.                     n= j->v->v_length - j->v_index;
  1349.                     cur_cmd= j->v->v + j->v_index;
  1350.                     goto exe_loop;
  1351.                 }
  1352.             }
  1353.             break;
  1354.  
  1355.         case 'w':
  1356.             ck_fwrite(line.text,1,line.length,cur_cmd->x.io_file);
  1357.             break;
  1358.  
  1359.         case 'x':
  1360.             {
  1361.                 struct line tmp;
  1362.  
  1363.                 tmp=line;
  1364.                 line=hold;
  1365.                 hold=tmp;
  1366.             }
  1367.             break;
  1368.  
  1369.         case 'y':
  1370.             {
  1371.                 unsigned char *p,*e;
  1372.  
  1373.                 for(p=(unsigned char *)(line.text),e=p+line.length;p<e;p++)
  1374.                     *p=cur_cmd->x.translate[*p];
  1375.             }
  1376.             break;
  1377.  
  1378.         default:
  1379.             panic("INTERNAL ERROR: Bad cmd %c",cur_cmd->cmd);
  1380.         }
  1381.         if(end_cycle)
  1382.             break;
  1383.     }
  1384. }
  1385.  
  1386.  
  1387. /* Return non-zero if the current line matches the address
  1388.    pointed to by 'addr'. */
  1389. int
  1390. match_address(addr)
  1391. struct addr *addr;
  1392. {
  1393.     switch(addr->addr_type) {
  1394.     case ADDR_NULL:
  1395.         return 1;
  1396.     case ADDR_NUM:
  1397.         return (input_line_number==addr->addr_number);
  1398.  
  1399.     case ADDR_REGEX:
  1400.         return (re_search(addr->addr_regex,
  1401.                   line.text,
  1402.                   line.length-1,
  1403.                   0,
  1404.                   line.length-1,
  1405.                   (struct re_registers *)0)>=0) ? 1 : 0;
  1406.  
  1407.     case ADDR_LAST:
  1408.         return (input_EOF) ? 1 : 0;
  1409.  
  1410.     default:
  1411.         panic("INTERNAL ERROR: bad address type");
  1412.         break;
  1413.     }
  1414.     return -1;
  1415. }
  1416.  
  1417. /* Read in the next line of input, and store it in the
  1418.    pattern space.  Return non-zero if this is the last line of input */
  1419.  
  1420. int
  1421. read_pattern_space()
  1422. {
  1423.     int n;
  1424.     char *p;
  1425.     int ch;
  1426.  
  1427.     p=line.text;
  1428.     n=line.alloc;
  1429.  
  1430.     if(feof(input_file))
  1431.         return 0;
  1432.     input_line_number++;
  1433.     replaced=0;
  1434.     for(;;) {
  1435.         if(n==0) {
  1436.             line.text=ck_realloc(line.text,line.alloc*2);
  1437.             p=line.text+line.alloc;
  1438.             n=line.alloc;
  1439.             line.alloc*=2;
  1440.         }
  1441.         ch=getc(input_file);
  1442.         if(ch==EOF) {
  1443.             if(n==line.alloc)
  1444.                 return 0;
  1445.             *p++='\n';
  1446.             --n;
  1447.             line.length=line.alloc-n;
  1448.             if(last_input_file)
  1449.                 input_EOF++;
  1450.             return 1;
  1451.         }
  1452.         *p++=ch;
  1453.         --n;
  1454.         if(ch=='\n') {
  1455.             line.length=line.alloc-n;
  1456.             break;
  1457.         }
  1458.     }
  1459.     ch=getc(input_file);
  1460.     if(ch!=EOF)
  1461.         ungetc(ch,input_file);
  1462.     else if(last_input_file)
  1463.         input_EOF++;
  1464.     return 1;
  1465. }
  1466.  
  1467. /* Inplement the 'N' command, which appends the next line of input to
  1468.    the pattern space. */
  1469. void
  1470. append_pattern_space()
  1471. {
  1472.     char *p;
  1473.     int n;
  1474.     int ch;
  1475.  
  1476.     p=line.text+line.length;
  1477.     n=line.alloc-line.length;
  1478.  
  1479.     input_line_number++;
  1480.     replaced=0;
  1481.     for(;;) {
  1482.         ch=getc(input_file);
  1483.         if(ch==EOF) {
  1484.             if(n==line.alloc)
  1485.                 return;
  1486.             *p++='\n';
  1487.             --n;
  1488.             line.length=line.alloc-n;
  1489.             if(last_input_file)
  1490.                 input_EOF++;
  1491.             return;
  1492.         }
  1493.         *p++=ch;
  1494.         --n;
  1495.         if(ch=='\n') {
  1496.             line.length=line.alloc-n;
  1497.             break;
  1498.         }
  1499.         if(n==0) {
  1500.             line.text=ck_realloc(line.text,line.alloc*2);
  1501.             p=line.text+line.alloc;
  1502.             n=line.alloc;
  1503.             line.alloc*=2;
  1504.         }
  1505.     }
  1506.     ch=getc(input_file);
  1507.     if(ch!=EOF)
  1508.         ungetc(ch,input_file);
  1509.     else if(last_input_file)
  1510.         input_EOF++;
  1511. }
  1512.  
  1513. /* Copy the contents of the line 'from' into the line 'to'.
  1514.    This destroys the old contents of 'to'.  It will still work
  1515.    if the line 'from' contains nulls. */
  1516. void
  1517. line_copy(from,to)
  1518. struct line *from,*to;
  1519. {
  1520.     if(from->length>to->alloc) {
  1521.         to->alloc=from->length;
  1522.         to->text=ck_realloc(to->text,to->alloc);
  1523.     }
  1524.     bcopy(from->text,to->text,from->length);
  1525.     to->length=from->length;
  1526. }
  1527.  
  1528. /* Append the contents of the line 'from' to the line 'to'.
  1529.    This routine will work even if the line 'from' contains nulls */
  1530. void
  1531. line_append(from,to)
  1532. struct line *from,*to;
  1533. {
  1534.     if(from->length>(to->alloc-to->length)) {
  1535.         to->alloc+=from->length;
  1536.         to->text=ck_realloc(to->text,to->alloc);
  1537.     }
  1538.     bcopy(from->text,to->text+to->length,from->length);
  1539.     to->length+=from->length;
  1540. }
  1541.  
  1542. /* Append 'length' bytes from 'string' to the line 'to'
  1543.    This routine *will* append bytes with nulls in them, without
  1544.    failing. */
  1545. void
  1546. str_append(to,string,length)
  1547. struct line *to;
  1548. char *string;
  1549. int length;
  1550. {
  1551.     if(length>to->alloc-to->length) {
  1552.         to->alloc+=length;
  1553.         to->text=ck_realloc(to->text,to->alloc);
  1554.     }
  1555.     bcopy(string,to->text+to->length,length);
  1556.     to->length+=length;
  1557. }
  1558.  
  1559. void
  1560. usage()
  1561. {
  1562.     fprintf(stderr, "\
  1563. Usage: %s [-nV] [+quiet] [+silent] [+version] [-e script] [-f script-file]\n\
  1564.        [+expression=script] [+file=script-file] [file...]\n", myname);
  1565.     exit(4);
  1566. }
  1567.  
  1568.  
  1569. #ifdef    WINDOWSNT
  1570. /*
  1571.  * a hack of yet another un*xism so that GNU stuff runs on Windows NT(tm)
  1572.  */
  1573.  
  1574. int
  1575. access(filename, mode)
  1576. char    *filename;
  1577. int    mode;
  1578. {
  1579.     HFILE    h;
  1580.     OFSTRUCT    o;
  1581.  
  1582.     if (!mode) {
  1583.         /*
  1584.          * test for existence with OpenFile
  1585.          */
  1586.  
  1587.         h = OpenFile(filename, &o, OF_EXIST);
  1588.  
  1589.         // what do we expect h to be?
  1590.  
  1591.         // will this test fail if the file exists but the caller is
  1592.         // not allowed any access to it?
  1593.  
  1594.         return -1;
  1595.     }
  1596.  
  1597.     if (mode & 4) {
  1598.         /*
  1599.          * check read access if we need to
  1600.          */
  1601.  
  1602.         h = OpenFile(filename, &o, OF_READ);
  1603.  
  1604.         if (h == HFILE_ERROR) {
  1605.             errno = EACCES;
  1606.  
  1607.             return -1;
  1608.         }
  1609.     }
  1610.  
  1611.     if (mode & 2) {
  1612.         /*
  1613.          * check write access if we need to
  1614.          */
  1615.  
  1616.         h = OpenFile(filename, &o, OF_WRITE);
  1617.  
  1618.         if (h == HFILE_ERROR) {
  1619.             errno = EACCES;
  1620.  
  1621.             return -1;
  1622.         }
  1623.     }
  1624.  
  1625.     if (mode & 1) {
  1626.         /*
  1627.          * we're hosed; I don't know an easy way to test for this.
  1628.          */
  1629.  
  1630.         // sorry...
  1631.  
  1632.         return -1;
  1633.     }
  1634. }
  1635. #endif
  1636.